#include "log.hpp"
#include <iostream>
#include<unordered_map>
const std::string default_dictpath="./dictionary.txt";
const std::string sep=":";
class dict
{
public:
    dict(std::string dict_path=default_dictpath)
    :_dict_path(dict_path)
    {}
    ~dict()
    {}
    void LoadDict()
    {
        std::string line;
        std::ifstream in(_dict_path);
        while(std::getline(in,line))
        {
            //处理字符串：
            int pos=line.find(sep);
            if(pos==std::string::npos)
            {
                //没有找到字串
                LOG(LogLevel::ERROR)<<"字典格式不匹配";
                continue;
            }
            std::string english=line.substr(0,pos);
            std::string chinese=line.substr(pos+sep.size());
            if(english.empty()||chinese.empty())
            {
                LOG(LogLevel::ERROR)<<"字典格式不匹配";
                continue;
            }
            _dict.insert(std::make_pair(english,chinese));
            std::cout<<"加载"<<line<<"成功"<<std::endl;
        }
        in.close();
    }
    std::string Translate(std::string info)
    {
        //返回值为迭代器
        auto pos=_dict.find(info);
        if(pos==_dict.end())
        {
            //没找到
            LOG(LogLevel::INFO)<<"字典未收录";
            return "None";
        }
        return pos->second;
    }
private:
    std::string _dict_path;
    std::unordered_map<std::string,std::string> _dict;
};